package org.abc.fund.controller.trade.response;

import lombok.Builder;
import lombok.Data;
import org.abc.fund.entity.trade.TradeOrder;
import org.abc.fund.entity.trade.TradeOrderDetail;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Builder
public class TradeOrderResponse {
  private Long id;
  private Long userId;
  private Long accountId;
  private TradeOrder.TradeType type;
  private TradeOrder.TradeStatus status;
  private BigDecimal amount;
  private LocalDateTime createdAt;
  private LocalDateTime approvedAt;
  private Long approverId;
  private List<TradeOrderDetailResponse> details;

  public static TradeOrderResponse fromEntity(TradeOrder order) {
    return TradeOrderResponse.builder()
        .id(order.getId())
        .userId(order.getUser().getId())
        .accountId(order.getAccount().getUserId())
        .type(order.getType())
        .status(order.getStatus())
        .amount(order.getAmount())
        .createdAt(order.getCreatedAt())
        .approvedAt(order.getApprovedAt())
        .approverId(order.getApprover() != null ? order.getApprover().getId() : null)
        .details(
            order.getDetails().stream()
                .map(TradeOrderDetailResponse::fromEntity)
                .collect(Collectors.toList()))
        .build();
  }
}
